<template>
  <div id="writecv">
    <Sidebar></Sidebar>
    <div style="height: 900px; width: 1500px; margin-left: 10%; margin-top: 5%">
      <el-container style="height: 100vh; border: 1px">
        <el-aside width="300px">
          <PerSidebar></PerSidebar>
        </el-aside>
        <el-main>
          <el-form
            ref="form"
            :model="form"
            label-width="80px"
            style="width: 600px; margin-left: 15%; margin-top: 100px"
          >
            <el-form-item>
              <textarea
                style="width: 530px; height: 400px; font-size: x-large"
                v-model="form.desc"
              ></textarea>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm">提交简历</el-button>
            </el-form-item>
          </el-form>
          <!-- 退出登录 -->
          <div>
            <el-button
              type="text"
              style="
                color: black;
                font-size: x-large;
                float: right;
                margin-top: 50px;
              "
              @click="Loginout"
              >退出登录</el-button
            >
          </div>
        </el-main>
      </el-container>
    </div>
  </div>
</template>
    <script>
import axios from "axios";
import Sidebar from "@/components/SeekerSidebar.vue";
import PerSidebar from "@/components/PersonSidebar.vue";
import { EventBus } from '@/util/event-bus.js';
export default {
  components: {
    Sidebar,
    PerSidebar,
  },
  data() {
    return {
      SeekerUserData: JSON.parse(sessionStorage.getItem("SeekerUser")),
      form: {
        desc: "",
      },
    };
  },
  created() {
    // 实例创建完成后立即获取文件内容
    this.fetchFileContent();
  },
  methods: {
    closeWebSocket() {
      EventBus.$emit('close-websocket-seeker');
    },
    fetchFileContent() {
      // 使用 Axios 从后端获取文件内容
      let data = new FormData();
      data.append("sinfo", this.SeekerUserData.sinfo);
      axios
        .post("http://localhost:8080/hqdx/seeker/showSeekerCv", data)
        .then((response) => {
          this.form.desc = response.data.data.items; // 更新Vue实例的数据
        })
        .catch((error) => {
          console.error("Error fetching file content:", error);
        });
    },
    submitForm() {
      let data = new FormData();
      data.append("susername", this.SeekerUserData.susername);
      data.append("cv", this.form.desc);
      axios
        .post("http://localhost:8080/hqdx/seeker/reviseCV", data)
        .then((response) => {
          alert("提交简历成功");
          this.fetchFileContent();
        })
        .catch((error) => {
          console.log(error);
        });
    },
    Loginout() {
      this.closeWebSocket();
      let data = new FormData();
      data.append("susername", this.SeekerUserData.susername);
      axios
        .post("http://localhost:8080/hqdx/seeker/loginout", data)
        .then((response) => {})
        .catch((error) => {
          console.log(error);
        });
      this.$router.push("/login");
      sessionStorage.setItem("LoginSeekerState", JSON.stringify(false)); //退出登录，状态记录
    },
  },
};
</script>
    <style scoped>
#writecv {
  background: linear-gradient(to right, #ffffff, #e0f7fa);
}
.el-aside {
  background-color: white;
  text-align: center;
}

.el-main {
  background: linear-gradient(to right, #ffffff, #e0f7fa);
  color: #333;
  text-align: center;
}
</style>